/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.biblioteca.dao.impl;

import com.biblioteca.beans.TipoPersonal;
import com.biblioteca.dao.TipoPersonalDAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author Jesus
 */
public class TipoPersonalDAOImpl extends JDBCSupport implements TipoPersonalDAO{

    @Override
    public int Insert(TipoPersonal tipoPersonal) throws Exception {
        String sql="INSERT INTO tipopersonal(descripcion) Values(?)";
        return this.jdbctemplate.update(sql, new Object[]{tipoPersonal.getDescripcion()});
    }

    @Override
    public List<TipoPersonal> getAll() throws Exception {
        String sql = "select * from tipopersonal";
        return jdbctemplate.query(sql, new RowMapper(){
            
            @Override
            public TipoPersonal mapRow(ResultSet rs, int rowNum) throws SQLException {
                TipoPersonal tipper = new TipoPersonal();
                tipper.setId(rs.getInt(1));
                tipper.setDescripcion(rs.getString(2));
                return tipper;
            }            
        });
    }

    @Override
    public int actualizar(TipoPersonal tipoPersonal) throws Exception {
        String sql="Update tipopersonal set   descripcion=?  where id= ?";
        return this.jdbctemplate.update(sql, new Object[]{tipoPersonal.getDescripcion(),tipoPersonal.getId()});
    }

    @Override
    public int eliminar(int id) throws Exception {        
        String sql = "delete from tipopersonal where id = ?";
        return this.jdbctemplate.update(sql, new Object[]{id});
    }

    @Override
    public TipoPersonal getid(int id) throws Exception {
        String sql="select * from tipopublicacion where id=?";
        return this.jdbctemplate.queryForObject(sql, new Object[]{id}, new RowMapper<TipoPersonal>() {

            @Override
            public TipoPersonal mapRow(ResultSet rs, int i) throws SQLException {
               TipoPersonal tipoper = new TipoPersonal();
                tipoper.setId(rs.getInt(1));
                tipoper.setDescripcion(rs.getString(2));
                return tipoper;
            }
        });
    }
    
}
